package com.ruoyi.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.http.HttpUtils;

/**
 * 获取地址类
 * 
 * @author ruoyi
 */
public class AddressUtils
{
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    // IP地址查询
    public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";

    // 未知地址
    public static final String UNKNOWN = "XX XX";

    public static String getRealAddressByIP(String ip)
    {
        // 内网不查询
        if (IpUtils.internalIp(ip))
        {
            return "内网IP";
        }
        if (RuoYiConfig.isAddressEnabled())
        {
            try
            {
                String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
                if (StringUtils.isEmpty(rspStr))
                {
                    log.error("获取地理位置异常 {}", ip);
                    return UNKNOWN;
                }
                JSONObject obj = JSONObject.parseObject(rspStr);
                String region = obj.getString("pro");
                String city = obj.getString("city");
                return String.format("%s %s", region, city);
            }
            catch (Exception e)
            {
                log.error("获取地理位置异常 {}", e);
            }
        }
        return UNKNOWN;
    }

    public static void main(String[] args) {
        String aa = "{\"body\":\"{\\\"ant_merchant_expand_indirect_zftorder_query_response\\\":{\\\"code\\\":\\\"10000\\\",\\\"msg\\\":\\\"Success\\\",\\\"orders\\\":[{\\\"smid\\\":\\\"2088240853566014\\\",\\\"sub_confirm\\\":\\\"CREATE\\\",\\\"face_pre_auth\\\":\\\"false\\\",\\\"fk_audit_memo\\\":\\\"批量审理\\\",\\\"apply_time\\\":\\\"2024-04-04 01:17:07\\\",\\\"app_pre_auth\\\":\\\"false\\\",\\\"kz_audit\\\":\\\"SKIP\\\",\\\"fk_audit\\\":\\\"PASS\\\",\\\"order_id\\\":\\\"2024040400502000000036600667\\\",\\\"apply_type\\\":\\\"ZHIFUTONG_CREATE_V2\\\",\\\"status\\\":\\\"031\\\"}]},\\\"alipay_cert_sn\\\":\\\"4d63d851370cb7ca136595ee60141f03\\\",\\\"sign\\\":\\\"LiotpI4HBjywmB54ucOC8cH1tI50yatsGA7lcljiH67j5yAcNgYdbWzqxOmFO/2+YMwZyyEMFLgfGjs4LhrC1aUyxhiJZ6DqFqar9NnykyO+I8uJ5OqWIlp0JdAr9UupzzkSEAm5cL3bHY82vAIHyPIUWJgUHVswT0YaZA1VygLraznbYlnLEDq5bgxTDmF2Wmv7urmuKh2+/KjWVmOq6LzMgk/oSRaTUm0dVam5PQBE0XL1BKwK7zuDHdPF3Y8M6Sr5qbFkBlHJzrzSdKsrk6Rk09bZiHxE8SHIWqaX6AjAXbi8K7M3WvdlFugrGx7c5/CL3BkGeBT7VQeJHYqWJg==\\\"}\",\"code\":\"10000\",\"errorCode\":\"10000\",\"msg\":\"Success\",\"orders\":[{\"appPreAuth\":\"false\",\"applyTime\":1712164627000,\"applyType\":\"ZHIFUTONG_CREATE_V2\",\"facePreAuth\":\"false\",\"fkAudit\":\"PASS\",\"fkAuditMemo\":\"批量审理\",\"kzAudit\":\"SKIP\",\"orderId\":\"2024040400502000000036600667\",\"smid\":\"2088240853566014\",\"status\":\"031\",\"subConfirm\":\"CREATE\"}],\"params\":{\"biz_content\":\"{  \\\"order_id\\\":\\\"2024040400502000000036600667\\\"}\"},\"success\":true}";
        JSONObject rs = JSONObject.parseObject(aa);
        JSONObject body = rs.getJSONObject("body");
        JSONObject ant_merchant_expand_indirect_zftorder_query_response = rs.getJSONObject("body");
        String code = ant_merchant_expand_indirect_zftorder_query_response.getString("code");

    }
}
